// pages/hospital_detail/index.js
var router = require('../../utils/router/index.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    indicatorDots: true,
    autoplay: true,
    current: 0,
    interval: 3000,
    duration: 200,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const data = router.extract(options);
    console.log(data.id); // { id: '123', type: 1 }
    this.get_hospital_by_id(data.id)
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**
   * 医院详情详情页数据
   */
  get_hospital_by_id: function(id){
    wx.showNavigationBarLoading();
    wx.showLoading({
      title: '加载中',
    })
    var that = this
    wx.cloud.callFunction({
      // 云函数名称
      name: 'hospital',
      // 传给云函数的参数
      data:{
        "action": "get_hospital_by_id",
        "id": id
      }
    })
    .then(res => {
      wx.hideNavigationBarLoading();
      wx.hideLoading()
      console.log(res.result.data) // 3

      var hospital_need = res.result.data.hospital_need
      for (var i = 0; i < hospital_need.length; i++) {
        // hospitalLists.push(hospitalList[i].hospital_name);
        hospital_need[i].i = i
        // console.log(hospital_need[i]);
      }

      that.setData({
        hospitaldetail:res.result.data,
      })
    })
    .catch(console.error);
  },

  /**
   * 现有库存
   */
  bind_have_count_Input: function(e){
    console.log(e)
    this.setData({
      bind_have_count_Input: e.detail.value
    })
  },

  /**
   * 日使用量
   */
  bind_user_count_Input: function(e){
    this.setData({
      bind_user_count_Input: e.detail.value
    })
  },

  /**
   *  更新医院信息
   */
  bind_update_hospital: function(){
    var that = this
      wx.showNavigationBarLoading();
      wx.showLoading({
        title: '加载中',
      })
      wx.cloud.callFunction({
        // 云函数名称
        name: 'hospital',
        // 传给云函数的参数
        data: 
        {
          "action": "operation_hospital_info",
          "_id":that.data.hospitaldetail._id,
          "hospital_name": that.data.hospitaldetail.hospital_name,
          "hospital_url": that.data.hospitaldetail.hospital_url,
          "hospital_phone": that.data.hospitaldetail.hospital_phone,
          "hospital_mobile": that.data.hospitaldetail.hospital_mobile,
          "hospital_address": that.data.hospitaldetail.hospital_address,
          "hospital_public": that.data.hospitaldetail.hospital_public,
          "hospital_logo": that.data.hospitaldetail.hospital_logo,
          "hospital_content": that.data.hospitaldetail.hospital_content,
          "hospital_need": that.data.hospitaldetail.hospital_need
        }
      })
      .then(res => {
        console.log(res.result) // 3
        if(res.result.errMsg == "collection.update:ok"){
          wx.switchTab({
            url: '../broadcastlist/index',
          })
        }
      })
      .catch(console.error);
      wx.hideNavigationBarLoading();
      wx.hideLoading()
  },

  /**
   * 修改库存
   */
  bind_get_stock_count: function(e){
    var that = this
    // console.log(e.currentTarget.id)
    that.data.hospitaldetail.hospital_need[e.currentTarget.id].stock 
    = that.data.bind_have_count_Input
    // console.log(that.data.hospitaldetail)
  },

  /**
   *  修改日使用量
   */
  bind_get_daily_usage_count: function(e){
    var that = this
    // console.log(e.currentTarget.id)
    that.data.hospitaldetail.hospital_need[e.currentTarget.id].daily_usage 
    = that.data.bind_user_count_Input
    // console.log(that.data.hospitaldetail)
  }

})